﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.Windows.Forms;
using System.Windows.Forms.Layout;
using NetJsWire.Util;
using Button = NetJsWire.Widgets.Ui.Form.Button;

namespace NetJsWire.Widgets.Ui.Window
{
    internal class WindowLayoutEngine : LayoutEngine
    {
        public override bool Layout( object container, LayoutEventArgs layoutEventArgs )
        {
            //return new System.Windows.Forms.Layout.

            var _win = container as Window;

            // Use DisplayRectangle so that parent.Padding is honored.
            var parentDisplayRectangle = _win.DisplayRectangle;
            var nextControlLocation = parentDisplayRectangle.Location;

            var nextButtonLocation = parentDisplayRectangle.Location;

            //if ( _win.Layout == Ui.Layout.LayoutType.Auto )
            //{
            //    // start at 0, 0
            //    //
            //    int x = 0;
            //    int y = 0;
            //    int maxLineWidth = 0;

            //    foreach ( Control control in _win.Controls )
            //    {

            //        // If the bottom edge of this control is going to exceed the bottom
            //        // edge of the container, then increment "x" and reset "y"
            //        //
            //        if ( y + control.Height > _win.Height && maxLineWidth > 0 )
            //        {
            //            x += maxLineWidth;
            //            y = 0;
            //            maxLineWidth = 0;
            //        }

            //        // Determine the max width of any control on this line
            //        //
            //        maxLineWidth = Math.Max( control.Width , maxLineWidth );

            //        // Position the current control
            //        //
            //        control.Location = new Point( x, y );

            //        // Increment "y" by the height of the control and the margin
            //        //
            //        y += control.Height ;
            //    }

            //}

            //base.Layout( container, layoutEventArgs );


            if ( _win.ButtonAlign == ButtonAlign.Right )
            {
                nextButtonLocation.Offset( ( _win.Width - 20 ), 0 );
            }

            foreach ( Control c in _win.Controls )
            {
                if ( c is Button && ( c as Button ).AutoPositioned )
                {
                    nextButtonLocation.Y = _win.Height - c.Height - c.Margin.Top - _win.Padding.Top -
                                           _win.Padding.Bottom - 35;

                    if ( _win.ButtonAlign == ButtonAlign.Left )
                    {
                        nextButtonLocation.Offset( c.Margin.Left, -c.Margin.Bottom );
                        c.Location = nextButtonLocation;

                        nextButtonLocation.X += c.Size.Width + c.Margin.Right;
                    }
                    else if ( _win.ButtonAlign == ButtonAlign.Right )
                    {
                        nextButtonLocation.Offset( -( c.Width + c.Margin.Left + c.Margin.Right ), 0 );
                        c.Location = nextButtonLocation;
                    }
                }
            }

            return base.Layout( container, layoutEventArgs );
        }
    }
}